I now have two fully tested routines that deal with all the scenarios and data types I tried them with (hundreds of variations).
bool unassigned(_*&x){int*ppx=addr_ x;return*pUNASSIGNED==*ppx}
bool unassigned(_&x) {int*px=addr_ x;return*pUNASSIGNED==*px}
Note that these two routines must be declared in the exact order shown. Why is this so? Well - it turns out that the same DXL Interpreter behaviour that led to the Equal-not-Equal-Bug bug, is also at play here! Note that the two routines deal with very specific different scenarios: The first handles checking whether pointers are assigned. The second checks whether non-pointer variables are assigned. Omit either one, and you lose half the desired functionality. You can verify that both routines are correctly used either by inserting differentiating print statements, or by simply toggling the "==" test to "!=" and watching them fail.
However: if you swap the declaration order, so that the routine for handling variables is declared first, then you will discover that it never gets called! Despite this, the expected correct results still occur for all the same hundreds of tested scenarios! How is this possible?
Whilst you cannot remove the unused (uncalled) routine - it stops variables checks being parsed - you can in fact reduce it to a forward declaration. Indeed, one that you never fulfil. And herein lies the clue....
bool unassigned(_&) // Never actually fulfilled!
bool unassigned(_*&x){int*ppx=addr_ x;return*pUNASSIGNED==*ppx}
The parser never looks beyond the second routine to fulfil the forward declaration, because it is blind to the difference! Thus you do not get a parser error or warning. During execution, the same algorithm looks for a match to suit the source variables. This it finds in the forward declaration. It thus coerces the variables to match the calling parameter mechanism (because the declaration expects by-reference parameters). Thus it now has pointer sources. So it then looks to see if there is a later declared routine that matches this specification - and it finds the pointer processing routine! Thus it calls it!
In this particular case, the outcome is nonetheless sane! This is because we are using addr_ to do an unchecked conversion into a pointer-pointer so that we can check if the pointer is unassigned. By pure chance, this addr_ call, when applied to referenced variable sources rather than pointers, just happens to place the variable where we were expecting a pointer to test for lack of assignment. Thus by pure chance, it actually works!
All that said and done... I opt for using the first example at the top of the post, because it does not rely upon "featured behaviour" (bug, I say!) in order to work. Even so, this is a fine example of how declaration order can fundamentally alter the behaviour of your program!
P.S. Oh - you also need this: const int UNASSIGNED const int *pUNASSIGNED=&UNASSIGNED
AlexTidmarsh - Sun Nov 30 04:15:01 EST 2014 |